package com.ty.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ty.mapper.OddMeetMapper;
import com.ty.pojo.OddMeet;
import com.ty.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class OddMeetServiceImpl implements OddMeetService{
    @Autowired
    private OddMeetMapper oddMeetMapper;

    @Override
    public void addOddMeet(OddMeet oddMeet) {
        oddMeetMapper.insert(oddMeet);
    }

    @Override
    public void updateOddMeet(OddMeet oddMeet) {
        oddMeetMapper.updateById(oddMeet);
    }

    @Override
    public void updateState(Integer id) {
        QueryWrapper<OddMeet> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        OddMeet oddMeet = new OddMeet();
        oddMeet.setState(0);
        oddMeetMapper.update(oddMeet, queryWrapper);
    }

    @Override
    public PageResult getOddMeetList(PageResult pageResult, OddMeet oddMeet) {
        IPage<OddMeet> iPage = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        QueryWrapper<OddMeet> queryWrapper = new QueryWrapper<>();
        boolean getName = StringUtils.hasLength(oddMeet.getOddName());
        boolean getType;
        if (oddMeet.getOddType() == null || "".equals(oddMeet.getOddType())){
            getType = false;
        }else {
            getType = true;
        }
        queryWrapper.and(Wrapper -> Wrapper.like(getName,"odd_name", oddMeet.getOddName())
                .eq(getType,"odd_type",oddMeet.getOddType())
                .eq("state", 1));
        iPage = oddMeetMapper.selectPage(iPage, queryWrapper);
        List<OddMeet> records = iPage.getRecords();
        long total = iPage.getTotal();
        return pageResult.setRows(records).setTotal(total);
    }


}
